<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Basic usage</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="java.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.java.html">Java 函数</a></div>
 <div class="up"><a href="java.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="java.examples-basic" class="section">
  <h2 class="title">Basic usage</h2>
  <p class="para">
   <div class="example" id="example-4290">
    <p><strong>Example #1 Java Example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;get&nbsp;instance&nbsp;of&nbsp;Java&nbsp;class&nbsp;java.lang.System&nbsp;in&nbsp;PHP<br /></span><span style="color: #0000BB">$system&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.lang.System'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;demonstrate&nbsp;property&nbsp;access<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'Java&nbsp;version='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$system</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.version'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">'Java&nbsp;vendor='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$system</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.vendor'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">'OS='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$system</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'os.name'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&nbsp;'&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$system</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'os.version'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&nbsp;on&nbsp;'&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$system</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'os.arch'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&nbsp;&lt;br&nbsp;/&gt;'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;java.util.Date&nbsp;example<br /></span><span style="color: #0000BB">$formatter&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.text.SimpleDateFormat'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"EEEE,&nbsp;MMMM&nbsp;dd,&nbsp;yyyy&nbsp;'at'&nbsp;h:mm:ss&nbsp;a&nbsp;zzzz"</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$formatter</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.util.Date'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-4291">
    <p><strong>Example #2 AWT Example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;This&nbsp;example&nbsp;is&nbsp;only&nbsp;intended&nbsp;to&nbsp;be&nbsp;run&nbsp;using&nbsp;the&nbsp;CLI.<br /><br /></span><span style="color: #0000BB">$frame&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.awt.Frame'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$button&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.awt.Button'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Hello&nbsp;Java&nbsp;World!'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #DD0000">'North'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$button</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">validate</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pack</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">visible&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">True</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$thread&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.lang.Thread'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$thread</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">10000</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dispose</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   Notes:

   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      <em>new Java()</em> will create an instance of a class if 
      a suitable constructor is available. If no parameters are passed and 
      the default constructor is useful as it provides access to classes 
      like <em>java.lang.System</em> which expose most of their 
      functionality through static methods.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      Accessing a member of an instance will first look for bean properties
      then public fields. In other words, <em>print $date.time</em>
      will first attempt to be resolved as <em>$date.getTime()</em>,
      then as <em>$date.time</em>.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      Both static and instance members can be accessed on an object with
      the same syntax. Furthermore, if the java object is of type
      <em>java.lang.Class</em>, then static members of the class 
      (fields and methods) can be accessed.
     </span>
    </li>
    <li class="listitem">
     <p class="para">
      Exceptions raised result in PHP warnings, and <strong><code>NULL</code></strong> results. The
      warnings may be eliminated by prefixing the method call with an
      &quot;@&quot; sign. The following APIs may be used to retrieve and reset
      the last error:
      <ul class="itemizedlist">
       <li class="listitem"><span class="simpara"><span class="function"><a href="function.java-last-exception-get.html" class="function">java_last_exception_get()</a></span></span></li>
       <li class="listitem"><span class="simpara"><span class="function"><a href="function.java-last-exception-clear.html" class="function">java_last_exception_clear()</a></span></span></li>
      </ul>
     </p>
    </li>
    <li class="listitem">
     <span class="simpara">
      Overload resolution is in general a hard problem given the
      differences in types between the two languages. The PHP Java
      extension employs a simple, but fairly effective, metric for
      determining which overload is the best match.
     </span>
     <span class="simpara">
      Additionally, method names in PHP are not case sensitive, potentially
      increasing the number of overloads to select from.
     </span>
     <span class="simpara">
      Once a method is selected, the parameters are coerced if necessary, 
      possibly with a loss of data (example: double precision floating point
      numbers will be converted to boolean).
      
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      In the tradition of PHP, arrays and hashtables may pretty much
      be used interchangeably. Note that hashtables in PHP may only be
      indexed by integers or strings; and that arrays of primitive types
      in Java can not be sparse. Also note that these constructs are
      passed by value, so may be expensive in terms of memory and time.
     </span>
    </li>
   </ul>
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="java.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.java.html">Java 函数</a></div>
 <div class="up"><a href="java.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
